package com.pic.mgt.file.io;

import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.util.LinkedList;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;

public class OneLineContentFileBuilder implements FileBuilder {
	private List<String> oneLineList;
	
	public OneLineContentFileBuilder() {
		oneLineList = new LinkedList<String>();
	}
	
	@Override
	public synchronized void append(String str) {
		oneLineList.add(str);
	}
	
	@Override
	public synchronized void buildFile(File file) throws IOException {
		OutputStream output = null;
		try {
			// throws IOException
			output = FileUtils.openOutputStream(file);
			for(String content : oneLineList) {
				// throws IOException
				IOUtils.write(content, output);
			}
		} finally {
			oneLineList.clear();
			
			if (output != null) {
				output.close();
			}
		}		
	}
}
